<?php

namespace Helper;

/**
 * 网路请求帮助类
 * Class HttpHelper
 * @package Helper
 */
class NetworkHelper extends BaseHelper
{


    /**
     * 地址拼接
     * @param $domain
     * @param string $path
     * @param string $str
     * @param bool $http
     * @return mixed|string
     */
    public static function siteUrl($domain, $path = '', $str = '', $http = false)
    {
        //1.处理HTTP HTTPS问题
        if ($http) {
            $http = 'http://';
        } else {
            if (self::isHttps()) {
                $http = 'https://';
            } else {
                $http = 'http://';
            }
        }

        if ($domain == '') {
            $domain = $_SERVER['HTTP_HOST'];
        }
        //2.判断是否只是处理域名URL并组装
        if ($path == '' AND $str == '') {
            $domain = str_replace("http://", "", $domain);
            $domain = str_replace("https://", "", $domain);
            $url = $domain;
        } elseif ($str == '') {
            $domain = self::getDomainByUrl($domain);
            $url = $domain . '/' . $path;
        } elseif ($path == '') {
            $domain = self::getDomainByUrl($domain);
            $url = $domain . '/?' . $str;
        } else {
            $domain = self::getDomainByUrl($domain);
            $url = $domain . '/' . $path . '/?' . $str;
        }
        //4.去 // ??
        $url = str_replace("//", "/", $url);
        $url = str_replace("??", "?", $url);
        $url = $http . $url;
        return $url;
    }

    /**
     * 通过URL获取域名
     * @param $url
     * @return mixed|string
     */
    public static function getDomainByUrl($url)
    {
        $domain = '';
        if ($url) {
            $url = str_replace("http://", "", $url);
            $url = str_replace("https://", "", $url);
            if ($url) {
                $strdomain = explode("/", $url);
                if (is_array($strdomain) AND !empty($strdomain) AND isset($strdomain[0])) {
                    $domain = $strdomain[0];
                }
            }
        }
        return $domain;
    }

    /**
     * 是否为https
     * @return bool
     */
    public static function isHttps()
    {
        if (isset($_SERVER['HTTPS']) AND !empty($_SERVER['HTTPS']) AND strtolower($_SERVER['HTTPS']) !== 'off') {
            return TRUE;
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) AND strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') {
            return TRUE;
        } elseif (isset($_SERVER['HTTP_FRONT_END_HTTPS']) AND !empty($_SERVER['HTTP_FRONT_END_HTTPS']) AND strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
            return TRUE;
        } elseif (isset($_SERVER['SERVER_PORT']) AND $_SERVER['SERVER_PORT'] == 443) {
            return TRUE;
        } elseif (isset($_SERVER['HTTP_X_CLIENT_SCHEME']) AND strtolower($_SERVER['HTTP_X_CLIENT_SCHEME']) == 'https') {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * Get client IP Address
     */
    public static function getClientIP()
    {
        if (isset($_SERVER['HTTP_ALI_CDN_REAL_IP']) && empty($_SERVER['HTTP_ALI_CDN_REAL_IP']) === false) {
            $ip = $_SERVER["HTTP_ALI_CDN_REAL_IP"];
        } elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && empty($_SERVER["HTTP_X_FORWARDED_FOR"]) === false) {
            $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } elseif (isset($_SERVER["HTTP_CLIENT_IP"]) && empty($_SERVER["HTTP_CLIENT_IP"]) === false) {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        } elseif (isset($_SERVER["REMOTE_ADDR"]) && empty($_SERVER["REMOTE_ADDR"]) === false) {
            $ip = $_SERVER["REMOTE_ADDR"];
        } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        } elseif (getenv("HTTP_CLIENT_IP")) {
            $ip = getenv("HTTP_CLIENT_IP");
        } elseif (getenv("REMOTE_ADDR")) {
            $ip = getenv("REMOTE_ADDR");
        } else {
            $ip = "Unknown";
        }
        //只取第一个
        $ip_array = explode(',', $ip);
        return $ip_array[0];
    }

    /**
     *  Get user broswer type
     */
    public static function getUserAgent()
    {
        $user_agent = '';
        $server_user_agent = $_SERVER["HTTP_USER_AGENT"];
        if (empty($server_user_agent)) {
            return '';
        } elseif (strpos($server_user_agent, "MSIE")) {
            if (strpos($server_user_agent, "MSIE 6.0")) {
                $user_agent = 'IE6';
            } elseif (strpos($server_user_agent, "MSIE 7.0")) {
                $user_agent = 'IE7';
            } elseif (strpos($server_user_agent, "MSIE 8.0")) {
                $user_agent = 'IE8';
            } elseif (strpos($server_user_agent, "MSIE 9.0")) {
                $user_agent = 'IE9';
            } elseif (strpos($server_user_agent, "MSIE 10.0")) {
                $user_agent = 'IE10';
            } else {
                $user_agent = 'IE';
            }
        } elseif (strpos($server_user_agent, "Netscape")) {
            $user_agent = 'Netscape';
        } elseif (strpos($server_user_agent, "Firefox")) {
            $user_agent = 'Firefox';
        } elseif (strpos($server_user_agent, "MicroMessenger")) {
            $user_agent = 'Weixin';
        } elseif (strpos($server_user_agent, "Chrome")) {
            if (strpos($server_user_agent, "UCBrowser")) {
                $user_agent = 'UCBrowser';
            } elseif (strpos($server_user_agent, "QQBrowser")) {
                $user_agent = 'QQBrowser';
            } elseif (strpos($server_user_agent, "BIDUBrowser")) {
                $user_agent = 'BIDUBrowser';
            } elseif (strpos($server_user_agent, "LBBROWSER")) {
                $user_agent = 'LBBROWSER';
            } elseif (strpos($server_user_agent, "MetaSr")) {
                $user_agent = 'SOGOU';
            } elseif (strpos($server_user_agent, "Maxthon")) {
                $user_agent = 'Maxthon';
            } elseif (strpos($server_user_agent, "TheWorld")) {
                $user_agent = 'TheWorld';
            } elseif (strpos($server_user_agent, "Edge")) {
                $user_agent = 'IE Edge';
            } else {
                $user_agent = 'Chrome';
            }
            return $user_agent;
        } elseif (strpos($server_user_agent, "Safari")) {
            $user_agent = 'Safari';
        } elseif (strpos($server_user_agent, "Opera")) {
            $user_agent = 'Opera';
        } elseif (strpos($server_user_agent, "UCWEB")) {
            $user_agent = 'UCWEB';
        } else {
            $user_agent = 'other';
        }
        return $user_agent;
    }

    public static function isWeixin()
    {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        }
        return false;
    }


    public static function getUserOS()
    {
        if (empty($_SERVER['HTTP_USER_AGENT'])) {
            return 'Unknown';
        }

        $os = '';
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);

        if (strpos($agent, 'win') !== false) {
            if (strpos($agent, 'nt 10.0') !== false) {
                $os = 'Windows 10';
            } elseif (strpos($agent, 'nt 6.1') !== false) {
                $os = 'Windows 7';
            } elseif (strpos($agent, 'nt 5.1') !== false) {
                $os = 'Windows XP';
            } elseif (strpos($agent, 'nt 5.2') !== false) {
                $os = 'Windows 2003';
            } elseif (strpos($agent, 'nt 5.0') !== false) {
                $os = 'Windows 2000';
            } elseif (strpos($agent, 'nt 6.0') !== false) {
                $os = 'Windows Vista';
            } elseif (strpos($agent, 'nt') !== false) {
                $os = 'Windows NT';
            }
        } elseif (strpos($agent, 'android') !== false) {
            $os = 'Android';
        } elseif (strpos($agent, 'linux') !== false) {
            $os = 'Linux';
        } elseif (strpos($agent, 'mac') !== false) {
            $os = 'Apple';
        } else {
            $os = 'Unknown';
        }
        return $os;
    }



}